package com.jiamao.goplay.ui.discover.vm;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.bean.FindItem;
import com.jiamao.goplay.ui.discover.AddrDetailFragment;
import com.jiamao.goplay.ui.user.OthersUserInfoFragment;
import com.jiamao.goplay.util.StringUtils;
import com.tamsiree.rxkit.view.RxToast;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class FindItemVM extends MultiItemViewModel {


    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();

    public ObservableField<Integer> index = new ObservableField<>();


    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> age = new ObservableField<>();
    public ObservableField<String> sz = new ObservableField<>();
    public ObservableField<String> dis = new ObservableField<>();
    public ObservableField<String> city = new ObservableField<>();
    public ObservableField<String> des = new ObservableField<>();
    public ObservableField<String> zy = new ObservableField<>();


    public ObservableField<String> gototitle = new ObservableField<>();
    public ObservableField<String> goto_nums = new ObservableField<>();
    public ObservableField<String> goto_addrtitle = new ObservableField<>();
    public ObservableField<String> goto_city = new ObservableField<>();
    public ObservableField<String> goto_img = new ObservableField<>();

    public ObservableInt ypdBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt yqBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt jsyqBtnVisibility = new ObservableInt(View.GONE);

    public ObservableInt yyqBtnVisibility = new ObservableInt(View.GONE);

    public ObservableField<String> yqStr = new ObservableField<>("邀请他去");
    public ObservableInt yqnBtnVisibility = new ObservableInt(View.GONE);

    public ObservableInt zyBtnVisibility = new ObservableInt(View.GONE);
    public ObservableInt desBtnVisibility = new ObservableInt(View.GONE);

    public ObservableField<Integer> isVip = new ObservableField<>();
    public ObservableField<Integer> isShming = new ObservableField<>();
    public ObservableField<Integer> isRenXiang = new ObservableField<>();

    public FindItem item;

    public FindItemVM(@NonNull BaseViewModel viewModel, FindItem item) {
        super(viewModel);
        this.item = item;

        if (item == null) {
            return;
        }

        if (item.getCertification() != null) {
            isVip.set(item.getCertification().getIsVip() == 0 ? View.GONE : View.VISIBLE);
            isRenXiang.set(item.getCertification().getAvatarVerified() == 0 ? View.GONE : View.VISIBLE);
            isShming.set(item.getCertification().getRealNameVerified() == 0 ? View.GONE : View.VISIBLE);
        }

        name.set(item.getBaseInfo().getNickname()+"，"); //昵称
        age.set(item.getUserInfo().getAge() + "");
        sz.set(item.getUserInfo().getGalaxy());

        StringBuffer addr = new StringBuffer();

        if (!TextUtils.isEmpty(item.getLocation().getDistance())){
            addr.append(item.getLocation().getDistance()+"km");
            if (!TextUtils.isEmpty(item.getLocation().getCity())){
                addr.append("，");
                addr.append(item.getLocation().getCity());
            }
            if (!TextUtils.isEmpty(item.getLocation().getArea())){
                addr.append("，");
                addr.append(item.getLocation().getArea());
            }
        }else if(!TextUtils.isEmpty(item.getLocation().getCity())){
            addr.append(item.getLocation().getCity());
            if (!TextUtils.isEmpty(item.getLocation().getArea())){
                addr.append("，");
                addr.append(item.getLocation().getArea());
            }
        }
        city.set(addr.toString());

        if (!TextUtils.isEmpty(item.getUserInfo().getDescribe())){
            desBtnVisibility.set(View.VISIBLE);
            des.set(item.getUserInfo().getDescribe());
       //     KLog.d("aaaaaaaaa:"+item.getUserInfo().getDescribe());
        }else{
            desBtnVisibility.set(View.GONE);
        }




        if (!TextUtils.isEmpty(item.getBaseInfo().getIsActive()) && (item.getBaseInfo().getIsActive().equals("现在活跃") || item.getBaseInfo().getIsActive().equals("当前活跃"))){
            zy.set(item.getBaseInfo().getIsActive());
            zyBtnVisibility.set(View.VISIBLE);
        }else if (!TextUtils.isEmpty( item.getUserInfo().getHeight()) || item.getUserInfo().equals("0")){

            if (!TextUtils.isEmpty( item.getUserInfo().getWeight())){
                zy.set(item.getUserInfo().getHeight()+"/"+item.getUserInfo().getWeight());
            }else{
                zy.set(item.getUserInfo().getWeight());
            }
           // zy.set(item.getUserInfo().getHeight()+"/"+item.getUserInfo().getWeight());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if (!TextUtils.isEmpty( item.getUserInfo().getWeight()) ){

                zy.set(item.getUserInfo().getWeight());
            // zy.set(item.getUserInfo().getHeight()+"/"+item.getUserInfo().getWeight());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if(!TextUtils.isEmpty(item.getUserInfo().getIndustry()) && !TextUtils.isEmpty(item.getUserInfo().getPosition()) ){
            zy.set(item.getUserInfo().getIndustry()+"/"+item.getUserInfo().getPosition());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if(!TextUtils.isEmpty(item.getUserInfo().getIndustry()) ){

            if (!TextUtils.isEmpty( item.getUserInfo().getPosition())){
                zy.set(item.getUserInfo().getIndustry()+"/"+item.getUserInfo().getPosition());
            }else{
                zy.set(item.getUserInfo().getIndustry());
            }

          //  zy.set(item.getUserInfo().getIndustry()+"/"+item.getUserInfo().getPosition());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if(!TextUtils.isEmpty(item.getUserInfo().getUniversity())){
            zy.set(item.getUserInfo().getUniversity());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if(!TextUtils.isEmpty(item.getUserInfo().getCompany())  ){
            zy.set(item.getUserInfo().getCompany());
            zyBtnVisibility.set(View.VISIBLE);
        }
        else if(!TextUtils.isEmpty(item.getUserInfo().getInterest()) ){
            zy.set(item.getUserInfo().getInterest());
            zyBtnVisibility.set(View.VISIBLE);
        }else {
            zyBtnVisibility.set(View.GONE);
        }

    //    zy.set(item.getUserInfo().getIndustry());


        if (item.getPlaceWantGo() != null) {
            gototitle.set(item.getPlaceWantGo().getStatusDes());
            goto_img.set(item.getPlaceWantGo().getPlaceCover());
            goto_nums.set(StringUtils.formatBigNum(item.getPlaceWantGo().getInterestUserNum()) + "人想去");
            goto_city.set(item.getPlaceWantGo().getCity()+"，");
            goto_addrtitle.set(item.getPlaceWantGo().getPlaceName());

            yqStr.set("邀请"+(item.getBaseInfo().getGender()==1?"他":"她")+"去");
            switch (item.getPlaceWantGo().getStatus()) {
                case 0:
                    if (item.getBaseInfo().getGender()==1){
                        yqnBtnVisibility.set(View.VISIBLE);
                    }else {
                        yqBtnVisibility.set(View.VISIBLE);
                    }
                    //yqBtnVisibility.set(View.VISIBLE);
                    break;
                case 1:
                    jsyqBtnVisibility.set(View.VISIBLE);
                    break;
                case 2:
                    ypdBtnVisibility.set(View.VISIBLE);
                    break;
                case 3:
                    yyqBtnVisibility.set(View.VISIBLE);
                    break;
            }

        }

        //模拟10个条目，数据源可以来自网络
        if (item.getAvatarList() != null) {
            for (int i = 0; i < item.getAvatarList().size(); i++) {

                FindItemImageViewVM imgItem = new FindItemImageViewVM(viewModel, item.getAvatarList().get(i).getUrl(),item.getBaseInfo().getId());
                observableList.add(imgItem);

            }
        }


    }

    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();

            itemBinding.set(BR.viewModel, R.layout.discover_find_item_viewpage_image_layout);
        }
    });


    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

          //  RxToast.showToastShort( sz.get());
          //  sz.get()

          //   ToastUtils.showShort("我是头布局");
            if (item!=null  && item.getBaseInfo()==null){
                return;
            }
            Bundle bd = new Bundle();
            bd.putString("uid",item.getBaseInfo().getId());
            viewModel.startContainerActivity(OthersUserInfoFragment.class.getCanonicalName(),bd);
        }
    });

    //条目的点击事件
    public BindingCommand szClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            RxToast.showToastShort( sz.get());
            //  sz.get()

            //   ToastUtils.showShort("我是头布局");
        }
    });

    //条目的点击事件
    public BindingCommand itemAddrClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (item == null) {
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putString("id", item.getPlaceWantGo().getPlaceId());
            viewModel.startContainerActivity(AddrDetailFragment.class.getCanonicalName(), bundle);


            //  if (item!=null)((FindVM)viewModel).jieshouyaoqing(item.getPlaceWantGo().getInvID());

            //  ToastUtils.showShort("我是头布局");
        }
    });

    //条目的点击事件
    public BindingCommand itemNextClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ((FindVM) viewModel).uc.pSwitchEvent.setValue(index.get());
        }
    });


    //条目的点击事件
    public BindingCommand jsyqClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if (item != null) ((FindVM) viewModel).jieshouyaoqing(item.getPlaceWantGo().getInvID(),FindItemVM.this);
        }
    });

    //
    //条目的点击事件
    public BindingCommand yqClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (item != null)
                ((FindVM) viewModel).yaoqing(item.getPlaceWantGo().getPlaceId(), item.getPlaceWantGo().getUid(),FindItemVM.this);
            // viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });

    //条目的点击事件
    public BindingCommand ypdClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if (item != null) ((FindVM) viewModel).startChat(item.getBaseInfo().getId());
            //    NimUIKit.startP2PSession(viewModel.getApplication(), uid);
            //((AddrDetailVM)viewModel).yaoqing(uid);
            // viewModel.startContainerActivity(SearchFragment.class.getCanonicalName());
        }
    });


    public void updateNext(int position){
        index.set(position);
        itemNextClick.execute();
    }
}
